package com.wish;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.KeyEvent;
import java.util.*;

/**
 * @Author MT
 * @Date 2021/11/22 14:45
 * @Description: 发送微信消息
 */


public class SendMsg {
    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        Thread.sleep(5000);
        while (true) {
            if(i>=100){
                break;
            }
            sendOneMsg("别吠了，大狗二狗");
            i++;
        }
    }
    private static Robot getRobot() {
        // 创建Robot对象
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        return robot;
    }
    private static void sendOneMsg(String msg) {
        Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
        Transferable tText;
        //创建Robot对象
        Robot robot = getRobot();
        //延迟十秒，主要是为了预留出打开窗口的时间，括号内的单位为毫秒
        if (null != robot) {
            robot.delay(50);
            tText = new StringSelection(msg);
            clip.setContents(tText, null);
            // 按下了ctrl+v，完成粘贴功能
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_V);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.delay(50);
            //回车发送消息
            robot.keyPress(KeyEvent.VK_ENTER);
            robot.delay(50);
        }
    }
}
